from dataclasses import dataclass, field


@dataclass
class PrinterConfig:
    is_configured: bool = False
    name: str = ""
    dpi: int = 0
    paper_width_mm: int = 0
    paper_height_mm: int = 0


@dataclass
class BartenderConfig:
    bin_path: str = ""
    xml_path: str = ""


@dataclass
class LayoutConfig:
    name_font_size_percent: float = 0.15
    receipt_font_size_percent: float = 0.15
    barcode_height_percent: float = 0.55
    margin_percent: float = 0.03
    code_moudle_width: float = 0.3
    code_moudle_height_percent: float = 0.7
    code_font_size_percent: float = 0.225
    code_text_distance_percent: float = 0.3


@dataclass
class APPConfig:
    printer_config: PrinterConfig = field(default_factory=PrinterConfig)
    bartender_config: BartenderConfig = field(default_factory=BartenderConfig)
    layout_config: LayoutConfig = field(default_factory=LayoutConfig)

    @classmethod
    def from_dict(cls, data: dict):
        # 提取并验证 printer_config
        printer_data = data["printer_config"]
        printer_config = PrinterConfig(
            is_configured=printer_data["is_configured"],
            name=printer_data["name"],
            dpi=printer_data["dpi"],
            paper_width_mm=printer_data["paper_width_mm"],
            paper_height_mm=printer_data["paper_height_mm"],
        )

        # 提取并验证 bartender_config
        bartender_data = data["bartender_config"]
        bartender_config = BartenderConfig(
            bin_path=bartender_data["bin_path"],
            xml_path=bartender_data["xml_path"],
        )

        # 提取并验证 layout_config
        layout_data = data["layout_config"]
        layout_config = LayoutConfig(
            name_font_size_percent=layout_data["name_font_size_percent"],
            receipt_font_size_percent=layout_data["receipt_font_size_percent"],
            barcode_height_percent=layout_data["barcode_height_percent"],
            margin_percent=layout_data["margin_percent"],
            code_moudle_width=layout_data["code_moudle_width"],
            code_moudle_height_percent=layout_data["code_moudle_height_percent"],
            code_font_size_percent=layout_data["code_font_size_percent"],
            code_text_distance_percent=layout_data["code_text_distance_percent"],
        )

        return cls(
            printer_config=printer_config,
            bartender_config=bartender_config,
            layout_config=layout_config,
        )
